package com.rory.gooday.consumption;

import com.rory.gooday.common.model.Consumption;
import com.jfinal.plugin.activerecord.Page;
import com.rory.gooday.common.model.ConsumptionCol;
import com.rory.gooday.common.model.ConsumptionDate;

import java.util.List;

public class ConsumptionService {

	private final Consumption consumptionDao = new Consumption().dao();

	private final ConsumptionDate consumptionDateDao = new ConsumptionDate().dao();

	private final ConsumptionCol consumptionColDao = new ConsumptionCol().dao();

	public Page<ConsumptionDate> datePaginate(int pageNumber, int pageSize) {
		Page<ConsumptionDate> b = consumptionDateDao.paginate(pageNumber, pageSize, "SELECT *", "FROM consumption_date ORDER BY consumption_date DESC");
		return b;
	}
	public Page<Consumption> consumptionPaginate(int cid, int pageNumber, int pageSize) {
		Page<Consumption> b = consumptionDao.paginate(pageNumber, pageSize, "SELECT consumption.*,consumption_col.col_name,consumption_col.col_type", "FROM consumption left join consumption_col on consumption.col_id = consumption_col.id where date_id = "+cid+" order by consumption.date_id desc,consumption.id desc");
		return b;
	}

	public List<Consumption> allConsumptionList() {
		List<Consumption> b = consumptionDao.findAll();
		return b;
	}

	public List<ConsumptionCol> getConsumptionCols(int type){
		// 1 是 in
		// 2 是 out
		if (type == 1){
			return consumptionColDao.find("SELECT * FROM consumption_col WHERE col_type = 'in'");
		}else if(type == 2){
			return consumptionColDao.find("SELECT * FROM consumption_col WHERE col_type = 'out'");
		}else{
			return consumptionColDao.findAll();
		}
	}



	public Consumption findById(int id) {
		return consumptionDao.findById(id);
	}

	public void deleteById(int id) {
		consumptionDao.deleteById(id);
	}

	public boolean createConsumption(Consumption consumption){
		return consumption.save();
	}
}
